package com.handinfo.android.uicontrols.controls;

import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 标签控件
 * @author handinfo
 */
public class DWLabel extends DWControl
{

	private int m_text_color = Tools.WHITE;
	private int m_background_color = Tools.BLACK;
	private boolean m_is_background = true;
	public String[] m_contents = null;
	private DWFont m_font = DWFont.getDefaultFont();

	public DWLabel(String text)
	{
		setName(text);
		setShowWideHigh((int)m_font.stringWidth(m_name) + 4, (int)m_font.getFontHeight());
		nameToContents();
	}

	public void setShowWideHigh(int w, int h) {
		super.setShowWideHigh(w, h);
		nameToContents();
	}
	
	public void setName(String name) {
		super.setName(name);
		nameToContents();
	}
	
	public void resetWideHigh() {
		if (m_contents != null && m_name != null) {
			setShowWideHigh((int)m_font.stringWidth(m_name) + 4, (int)m_font.getFontHeight());
			m_contents = null;
			m_contents = new String[] {m_name};
		}
	}
	
	public void setFont(DWFont font) {
		m_font = font;
	}
	
	public void nameToContents() {
		if (m_name != null) {
			if ((int)m_font.stringWidth(m_name) > m_show_w) {
				m_contents = DWControlTools.splitString(m_name, m_font, DWControlTools.CHANGE_ROW, m_show_w, 0, false);
			} else {
				m_contents = null;
				m_contents = new String[] {m_name};
			}
		}
	}
	
	public void setTextColor(int color)
	{
		m_text_color = color;
	}

	public void setBackgroundColor(int color)
	{
		m_background_color = color;
	}
	
	public void setShowBackground(boolean show) {
		m_is_background = show;
	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x - 2, m_show_y - 2, m_show_w + 4, m_show_h + 4);
		if (m_is_background) {
			g.setColor(m_background_color);
			g.fillRect(m_show_x - 2, m_show_y - 2, m_show_w + 4, m_show_h + 4);
		}
		if (m_contents != null) {
			for (int i = 0; i < m_contents.length; i++) {
				g.drawShadowString(m_font, m_contents[i], DWControlsManager.COLOR_STROKE, m_text_color,
						m_show_x + m_show_w / 2,
						m_show_y + ((m_show_h - m_contents.length * m_font.getFontHeight()) / 2) + i * m_font.getFontHeight(), Tools.HCENTER_TOP);
			}
		}
		renderRect(g);
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		if (m_is_background) {
			g.setColor(m_background_color);
			g.fillRect(x + m_show_x - 2 - offsetx, y + m_show_y - 2 - offsety, m_show_w + 4, m_show_h + 4);
		}
		if (m_contents != null) {
			for (int i = 0; i < m_contents.length; i++) {
				g.drawShadowString(m_font, m_contents[i], DWControlsManager.COLOR_STROKE, m_text_color,
						x + m_show_x + m_show_w  / 2,
						y + m_show_y + ((m_show_h - m_contents.length * m_font.getFontHeight()) / 2) - offsety + i * m_font.getFontHeight(), Tools.HCENTER_TOP);
			}
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
	}

	public void refreshControl()
	{
	}
	public void logic()
	{

	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY) {
		return false;
	}

	public boolean doClick(float pointX, float pointY)
	{
		if (checkPointOnRect(pointX, pointY)) {
			if (m_listener != null) {
				m_listener.OnClick();
			}
		}
		return false;
	}

}
